/*
 * 文件名： ItemActivity.java
 * 描    述：商品列表页
 * 时    间：2015-3-25 下午5:47:21
 * 版    权：V1.0
 */
package com.miguo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

import com.miguo.activity.base.BaseListActivity;
import com.miguo.activity.callback.IGoods;
import com.miguo.activity.callback.IGoodsScroll;
import com.miguo.activity.callback.listener.GoodsScrollListener;
import com.miguo.adapter.GoodsAdapter;
import com.miguo.bean.CommentBean;
import com.miguo.bean.GoodsBean;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.component.BaseStatusView;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.RadioGroup;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.ui.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ItemActivity extends BaseListActivity<GoodsBean, GoodsAdapter> implements IGoodsScroll, IGoods<GoodsBean, GoodsAdapter>, OnClickListener,
        RadioGroup.OnCheckedChangeListener {

    private Intent in;

    private String cid = "0";
    private String sort = "default";
    private String url;

    private RadioButton mPrice;
    private ImageView mIvPrice;
    private FrameLayout mflPrice;
    private RadioGroup mItemSort;
    private BaseHeadView mBaseHead;

    private int PriceSort = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item);
        setGoodsCallBack(this);
        initExtra();
        findViewById();
        initView();
    }

    private void initExtra() {
        in = getIntent();
        cid = in.getStringExtra("id");
        if (in.hasExtra("url")) {
            url = in.getStringExtra("url");
        }
        //兼容旧版本
        if (in.hasExtra("type")) {
            String type = in.getStringExtra("type");
            if (type.equals("jiu")) {
                url = Url.toRoute(Url.Jiu.List, 1);
            } else {
                url = Url.toRoute(Url.Item.List, 1);
            }
        }
    }

    private RelativeLayout mContent;
    private BaseStatusView BaseStatus;

    @Override
    protected void findViewById() {
        super.findViewById();
        mflPrice = (FrameLayout) findViewById(R.id.flPrice);
        mflPrice.setOnClickListener(this);
        mIvPrice = (ImageView) findViewById(R.id.ivPrice);
        mPrice = (RadioButton) findViewById(R.id.price);
        mItemSort = (RadioGroup) findViewById(R.id.item_sort);
        mItemSort.setOnCheckedChangeListener(this);
        mContent = (RelativeLayout) findViewById(R.id.content_view);
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setActivity(this);
        mBaseHead.setBackBtn(isBackBtn);
        mBaseHead.setTitle(in.getStringExtra("title"));
        BaseStatus = (BaseStatusView) findViewById(R.id.base_status_view);
        BaseStatus.setContentView(mContent);
        BaseStatus.getRefreshView().setOnClickListener(this);
        BaseStatus.getNetworkView().setOnClickListener(this);
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.acquiesce:
                sort = "default";
                break;
            case R.id.volume:
                sort = "volume";
                break;
            case R.id.price:
                if (PriceSort % 2 == 1) {
                    sort = "price_asc";
                    mIvPrice.setImageResource(R.drawable.goods_sort_down);
                } else {
                    sort = "price_desc";
                    mIvPrice.setImageResource(R.drawable.goods_sort_up);
                }
                AjaxCallBack.show();
                initItem();
                PriceSort++;
                mPrice.setFocusable(false);
                mPrice.setClickable(false);
                return;
            case R.id.news:
                sort = "news";
                break;
        }
        AjaxCallBack.show();
        initItem();
        mIvPrice.setImageResource(R.drawable.goods_sort_unselected);
        mPrice.setFocusable(true);
        mPrice.setClickable(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.refresh_rl:
                this.onGoodsLoadData();
                break;
            case R.id.network_error_ll:
                this.onGoodsLoadData();
                break;
            case R.id.flPrice:
                if (PriceSort % 2 == 1) {
                    sort = "price_asc";
                    mIvPrice.setImageResource(R.drawable.goods_sort_down);
                } else {
                    sort = "price_desc";
                    mIvPrice.setImageResource(R.drawable.goods_sort_up);
                }
                PriceSort++;
                AjaxCallBack.show();
                initItem();
                break;
        }
        super.onClick(v);
    }

    private void initItem() {
        page = 1;
        isRefresh = true;
        this.onGoodsLoadData();
    }

    @Override
    public GoodsAdapter onGoodsLoadAdapter() {
        return new GoodsAdapter(mActivity);
    }

    @Override
    public List<GoodsBean> onGoodsDealList(JsonData data) {
        return CommentBean.GoodsData(data.optJson("item"));
    }

    @Override
    public void onGoodsLoadData() {
        Map<String, String> param = new HashMap<String, String>();
        param.put("cid", cid);
        param.put("page", page + "");
        param.put("order", sort);
        HttpClient.post(url, param, AjaxCallBack);
    }

    @Override
    public GoodsScrollListener onGoodsScroll() {
        return new GoodsScrollListener(this);
    }

    @Override
    public void onGoodsRefresh() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onGoodsFinish() {
        BaseStatus.setRefreshVisibility(View.GONE);
    }

    @Override
    public void onGoodsError() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onGoodsFail() {
        BaseStatus.setRefreshVisibility(View.VISIBLE);
    }

    @Override
    public void onScrollIdle() {
        imageLoader.resume();
    }

    @Override
    public void onScrollFling() {
        if (pauseOnFling) {
            imageLoader.pause();
        }
    }

    @Override
    public void onScrollTouchScroll() {
        if (pauseOnScroll) {
            imageLoader.pause();
        }
    }

    @Override
    public void onScrollTopPositiont() {
        // 判断滚动到顶部
        if (mToTopBtn.getVisibility() == View.VISIBLE) {
            mToTopBtn.setVisibility(View.GONE);
            mToTopBtn.startAnimation(mTopDownAnima);
        }
    }

    @Override
    public void onScrollFirstLastPosition() {
        // 第一次滚动至底部
        if (mToTopBtn.getVisibility() == View.GONE) {
            mToTopBtn.setVisibility(View.VISIBLE);
            mToTopBtn.startAnimation(mTopUpAnima);
        }
    }

    @Override
    public void onScrollLastPosition() {
        // 判断滚动到底部
        if (mToTopBtn.getVisibility() == View.GONE) {
            mToTopBtn.setVisibility(View.VISIBLE);
            mToTopBtn.startAnimation(mTopUpAnima);
        }
        if (pageSize >= page + 1) {
            page++;
            isRefresh = false;
            this.onGoodsLoadData();
        } else {
            Toast.show(mContext, "已经到底了!");
            pullToRefresh.onRefreshComplete();
        }
    }
}
